﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Data;

namespace MII_FIF.Layouts.MII_FIF.Master.Location
{
    public partial class AddLocation : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Generic.IsUserInGroup(Configuration.MasterGroupName))
            {
                if (Request.QueryString["ID"] != null)
                    ((Label)Header.FindControl("lblHeader")).Text = "Edit Location";
                else
                    ((Label)Header.FindControl("lblHeader")).Text = "Add Location";
                btnCancel.Click += btnCancel_Click;
                btnSubmit.Click += btnSubmit_Click;
                if (!IsPostBack)
                {
                    load();
                }
            }
            else
            {
                Response.Redirect(Generic.GenLink("/"));
            }
        }

        void btnSubmit_Click(object sender, EventArgs e)
        {
            lblError.Text = string.Empty;
            bool check = true;
            InventoryManagement.Location Location = new InventoryManagement.Location();
            if (txtKode.Text.Trim() == string.Empty)
            {
                check = false;
                lblError.Text += "Please Insert Kode Lokasi <br />";
            }
            if (txtNama.Text.Trim() == string.Empty)
            {
                check = false;
                lblError.Text += "Please Insert Nama Lokasi <br />";
            }

            if (check)
            {
                Location.Kode = txtKode.Text;
                Location.Nama = txtNama.Text;
                Location.IsDeleted = false;
                if (Request.QueryString["ID"] == null)
                {
                    Location.ID = Guid.NewGuid();
                    int stat = Location.Insert();
                    if (stat > 0)
                        Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/Master/Location/ViewLocation.aspx"));
                    else
                        lblError.Text += "Insert Failed";
                }
                else
                {
                    Location.ID = Guid.Parse(Request.QueryString["ID"].ToString());
                    Location.Select();
                    if (Location.IsDeleted.Value)
                    {
                        lblError.Text += "This item is already deleted <br />";
                    }
                    else
                    {
                        Location.Kode = txtKode.Text;
                        Location.Nama = txtNama.Text;
                        Location.IsDeleted = false;
                        int stat = Location.Update();
                        if (stat > 0)
                            Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/Master/Provider/ViewLocation.aspx"));
                        else
                            lblError.Text += "Update Failed";
                    }
                }
            }
        }

        void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/Master/Provider/ViewLocation.aspx"));
        }

        void load()
        {
            InventoryManagement.Location location = new InventoryManagement.Location();
            if (Request.QueryString["ID"] != null)
            {
                ViewState["ID"] = Request.QueryString["ID"].ToString();
                location.ID = Guid.Parse(ViewState["ID"].ToString());
                location.Select();
                txtKode.Text = location.Kode;
                txtNama.Text = location.Nama;
            }
        }
    }
}
